import { createRouter, createWebHashHistory, Router, RouteRecordRaw } from 'vue-router'
import { store } from "@/store";
import t from '@/utils/i18n'

export interface createRouter {
    history: Function,
    routes: any
}
export const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/index',
    },
    {
        path: '/',
        name: 'Home',
        component: () => import('@/layouts/Home.vue'),
        meta: {
            title: 'index',
            icon: "<shopping-outlined />",
            menu: true
        },
        children: [
            {
                path: '/index',
                name: 'Index',
                component: () => import('@/views/index/index.vue'),
                meta: {
                    title: 'presale',
                    icon: "ShoppingOutlined",
                    menu: true,
                }
            },
            {
                path: '/swap',
                name: 'Swap',
                component: () => import('@/views/swap/index.vue'),
                meta: {
                    title: 'swap',
                    icon: "SyncOutlined",
                    menu: true,
                }
            },
            {
                path: '/nft',
                name: 'NFT',
                component: () => import('@/views/index/index.vue'),
                meta: {
                    title: 'NFT',
                    icon: "SkinOutlined",
                    menu: true,
                }
            },
            {
                path: '/whitepaper',
                name: 'WhitePaper',
                component: () => import('@/views/index/index.vue'),
                meta: {
                    title: 'whitePaper',
                    icon: "FileWordOutlined",
                    menu: true,
                }
            },
        ]
    },
    {
        path: '/404',
        component: () => import('@/layouts/404.vue'),
        meta: {
            title: '404 Not Found',
            menu: false
        }
    }
]
const router: Router = createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (to.hash) {
            return {
                el: to.hash,
                behavior: 'smooth',
            }
        }
        if (savedPosition) {
            return savedPosition
        }
        return { top: 0 }
    }
})


router.addRoute({
    path: "/:catchAll(.*)",
    redirect: "/404",
    name: "NotFound",
})

router.beforeEach(((to, from, next) => {
    store.commit("setSpinning", true)
    if (typeof to.meta.title == "string") {
        document.title = t('nav.' + to.meta.title);
    }
    next();
}))
router.afterEach(((to, from, failure) => {
    store.commit("setSpinning", false)
}))

export default router